const HtmlWebpackPlugin = require('html-webpack-plugin'); //通过 npm 安装
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin') // vue-loader 插件，它的职责是将你定义过的其它规则复制并应用到 .vue 文件里相应语言的块
const webpack = require('webpack'); //访问内置的插件
const path = require('path');

const entry = require('./entry');
for(let key in entry) {
  entry[key].unshift('webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000')
}

function moreHtml() {
  let arr = [];
  for (let key in entry) {
    arr.push(new HtmlWebpackPlugin({
      template: './src/pages/' + key + '/index.html',
      title: key,
      chunks:[
        'wx',
        key
      ],
      filename: key + '.html'
    }))
  }
  return arr;
}

module.exports = {
    entry: {
      'wx': "./src/common/wx",
      ...entry
    },
    output: {
      path: __dirname +  '/dist',
      filename: '[name].bundle.js',
      publicPath: '/'
    },
    module: {
  		rules: [
        // {
        //   test: /(\.jsx|\.js)$/,
        //   use: {loader:'eslint-loader'},
        //   enforce: "pre", // 编译前检查
        //   exclude: /node_modules/
        // },
        //  使用vue-loader 加载 .vue 结尾的文件
  	    {
          test: /(\.jsx|\.js)$/,
          use: {
              loader: "babel-loader",
          },
          exclude: /node_modules/
  	    },
        {
          test: /\.vue$/,
          use: ['vue-loader']
        },
        {
          test: /(\.css|\.less)$/,
          use: [
              {
                loader: "style-loader"
              }, {
                loader: "css-loader"
              }, {
                  loader: "less-loader" // compiles Less to CSS
              }
          ]
        },
        {
          test: /\.(ttf|eot|woff|woff2)$/,
          loader: 'file-loader',
          options: {
            name: 'fonts/[name].[ext]',
          },
        },
        {
          test: /\.svg/,
          use: ['file-loader']
        }
  		]
  	},
    plugins: [
      new VueLoaderPlugin(),
      new ManifestPlugin(),
      new webpack.BannerPlugin('版权所有，翻版必究'),
      new webpack.NamedModulesPlugin(),
      new webpack.HotModuleReplacementPlugin(),//热加载插件
      ...moreHtml()
    ],
    devtool: 'source-map',
    mode: 'development',
    devServer: {
      contentBase: "./public",//本地服务器所加载的页面所在的目录
      port: 7777,
      historyApiFallback: true,//不跳转
      hot: true
    } 
};